package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IOrganizationInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.Collections;

/**
 * 作用:  组织架构信息控制层<br/>
 * 创建时间: 2021年05月12日 11时47分31秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "组织架构信息Web接口", tags = "系统管理——组织架构信息操作")
@RequestMapping(value = "/api/organization")
@RestController
public class OrganizationInfoController extends ControllerBase {

    @Autowired
    IOrganizationInfoService service;

    @ApiOperation(value = "查询组织架构信息", notes = "查询组织架构信息")
    @RequestMapping(value = "/unit", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryTreeList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询组织架构信息", notes = "查询组织架构信息")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public WebResult querySelectList(@RequestParam(value = "key", required = false) String key) {
        try {
            var searchData = service.queryListByFullNameKey(key);
            for (int i = 0; i < searchData.size(); i++) {
                var obj = searchData.get(i);
                if(obj.getLayer()>0) {
                    obj.setFullName(Tl.charCopyToLeft(obj.getFullName(),obj.getLayer(),"—"));
                }

            }
            return success("ok", searchData);
        } catch (Exception ee) {
            return error(ee);
        }

    }


    @ApiOperation(value = "查询组织架构信息", notes = "根据组织架构信息编号查询组织架构信息")
    @RequestMapping(value = "/unit/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存组织架构信息", notes = "查询/更新组织架构信息信息")
    @RequestMapping(value = "/unit", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveOrganizationInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "删除组织架构信息", notes = "根据组织架构编号删除组织架构信息")
    @RequestMapping(path = "/unit", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@ApiParam(value = "系统菜单编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
}